﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ipaidabribe.Code;

namespace ipaidabribe
{
    public partial class Default : System.Web.UI.Page
    {
        public int page = 1;
        public int totalpage;
        public bool sa = false;
        public string html_pager = "";
        public string city_filter = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["page"] != null)
            {
                int.TryParse(Request.QueryString["page"].ToString(), out page);
            }
            if (Request.QueryString["city"] != null)
            {
                city_filter = Request.QueryString["city"].ToString();
            }

            var db = new ipaidabribeEntities();
            var lastest = new List<Bribe>();
            
            if (city_filter == "")
                lastest = db.Bribes.Where(t => t.Enable != false).OrderByDescending(t => t.Id).Skip((page - 1) * 15).Take(15).ToList();
            else
                lastest = db.Bribes.Where(t => t.Enable != false && t.City.Contains(city_filter)).OrderByDescending(t => t.Id).Skip((page - 1) * 15).Take(15).ToList();


            foreach (var bribe in lastest)
            {
                if (HttpUtility.HtmlDecode(bribe.Content).Length > 400)
                {
                    bribe.Content = HttpUtility.HtmlDecode(bribe.Content);
                    var contentArr = bribe.Content.Split(' ');
                    bribe.Content = bribe.Content.Substring(0, bribe.Content.IndexOf(' ', 400)) + " ...";
                    bribe.Content += " <a href='/Detail.aspx?ID="+bribe.Id+"'><b>Xem tiếp</b></a>";
                }
                else
                {
                    bribe.Content = HttpUtility.HtmlDecode(bribe.Content);
                    
                }
                
                bribe.Content = bribe.Content.Replace("</div>", String.Empty);
                bribe.Content = bribe.Content.Replace("<div>", "<br>");
                
                double d = 0;
                bool abc = double.TryParse(bribe.AmountPaid, out d);
                if (abc)
                    bribe.AmountPaid = String.Format("{0:0,0}", d) + " VND";
                if (bribe.AmountPaid == "00 VND")
                    bribe.AmountPaid = "0 VND";
            }
            Repeater1.DataSource = lastest;
            Repeater1.DataBind();
            
            //phan trang html
            var countRow = 0;
            
            if (city_filter == "")
                lastest = db.Bribes.Where(t => t.Enable != false).OrderByDescending(t => t.Id).Skip((page - 1) * 15).Take(15).ToList();
            else
                lastest = db.Bribes.Where(t => t.Enable != false && t.City.Contains(city_filter)).OrderByDescending(t => t.Id).Skip((page - 1) * 15).Take(15).ToList();


            if (sa == false)
            {
                if (city_filter == "")
                    countRow = db.Bribes.Where(t => t.Enable != false).Count();
                else
                    countRow = db.Bribes.Where(t => t.Enable != false && t.City.Contains(city_filter)).Count();
            }
            else
            {
                countRow = db.Bribes.Count();
            }


            totalpage = countRow % 15 == 0 ? countRow / 15 : (countRow / 15) + 1;
            var current_page = page;
            var total_page = totalpage;
            //show pager
            var start = current_page - 5;
            if (start < 0)
            {
                start = 1;
            }
            var end = current_page + 5;
            if (current_page < 5)
            {
                end = current_page + 9;
            }
            if (end > total_page)
            {
                end = total_page;
            }
            //foeach

            //trang dau
            //trang truoc
            if (current_page > 1)
            {
                html_pager += "<a data-role=\"button\" title=\"\" href=\"Default.aspx?city="+city_filter+"\">« đầu</a>&nbsp;";
                html_pager += "<a data-role=\"button\" title=\"\" href=\"Default.aspx?city=" + city_filter + "&page=" + (current_page - 1) + "\">‹ trước</a>&nbsp;";
            }

            for (var i = start; i <= end; i++)
            {
                html_pager += "";
                if (i == current_page)
                {
                    html_pager += "<a style=\"color: #FF3300;\" data-role=\"button\" title=\"\" href=\"javascript:void(0);\">" + current_page + "</a>&nbsp;";
                }
                else
                {
                    html_pager += "<a data-role=\"button\" title=\"\" href=\"Default.aspx?city=" + city_filter + "&page=" + i + "\">" + i + "</a>&nbsp;";
                }
            }
            //sau va cuoi cung
            if (current_page < total_page)
            {
                html_pager += "<a data-role=\"button\" title=\"\" href=\"Default.aspx?city=" + city_filter + "&page=" + (current_page + 1) + "\">sau ›</a>&nbsp;";
                html_pager += "<a data-role=\"button\" title=\"\" href=\"Default.aspx?city=" + city_filter + "&page=" + total_page + "\">cuối »</a>&nbsp;";
            }
            //bind html            
        }
    }
}